let text = require('../../../../common/util/text.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    week:'',
    today_communication_skills_status:'',
    treatment_item_id:'',
    type:'',
    range:["领导","同事","朋友","家人","亲戚"],
    communication_object:'',//关系
    full_name:'',//称呼
    communicate_problems:'',//沟通的问题
    communicate_expectations:'',//沟通的预期
    is_controllable:-1,//控制范围
    expectation_analysis:'',//不可控分析
    say:'', //如何表达
    success_probability:'',//概率
    list:[] ,
    show:false,
    step:0,
    _id:'',
    practice_help:{},
    PracticeStatus:false,
    showtips:false,
    is_write:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
      let that =this;
      console.log(options)
      that.setData({
        week:Number(options.week),
        treatment_item_id:options.treatment_item_id,
        type:Number(options.type),
        today_communication_skills_status:options.today_communication_skills_status=='true'?true:false
      })
      that.xGetHelp()
  },

  /**
   *   监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    getApp().globalData.lock=false
    this.xInit()
    this.xToPracticeStatus()
  },
  xInit:function(week,id){
    let that =this;
    wx.cloud.callFunction({
      name: 'communication_skills',
      data: {
        action: "get_communication_skills"
      }
    }).then(res => {
      if(res.result.length>0 && that.data.today_communication_skills_status){
          let data= res.result[res.result.length-1]
          that.setData({
            _id:data._id,
            communication_object:data.communication_object,
            full_name:data.full_name,
            communicate_problems:data.communicate_problems,
            communicate_expectations:data.communicate_expectations,
            is_controllable:data.is_controllable,
            expectation_analysis:data.expectation_analysis,
            say:data.say,
            success_probability:data.success_probability,
          })
      }else{
        try {
          var value = wx.getStorageSync('communication_object')
          if (value) {
            that.setData({
              communication_object:value
            })
          }
        } catch (e) {}
        try {
          var value = wx.getStorageSync('full_name')
          if (value) {
            that.setData({
              full_name:value
            })
          }
        } catch (e) {}
        try {
          var value = wx.getStorageSync('communicate_problems')
          if (value) {
            that.setData({
              communicate_problems:value
            })
          }
        } catch (e) {}
        try {
          var value = wx.getStorageSync('communicate_expectations')
          if (value) {
            that.setData({
              communicate_expectations:value
            })
          }
        } catch (e) {}
        try {
          var value = wx.getStorageSync('is_controllable')
          if (value) {
            that.setData({
              is_controllable:value
            })
          }
        } catch (e) {}
        try {
          var value = wx.getStorageSync('expectation_analysis')
          if (value) {
            that.setData({
              expectation_analysis:value
            })
          }
        } catch (e) {}
        try {
          var value = wx.getStorageSync('say')
          if (value) {
            that.setData({
              say:value
            })
          }
        } catch (e) {}
        try {
          var value = wx.getStorageSync('success_probability')
          if (value) {
            that.setData({
              success_probability:value
            })
          }
        } catch (e) {}
      }
      
    })
  },
  xToPracticeStatus:function(){
    let that =this;
    wx.cloud.callFunction({
      name: 'practice_help',
      data: {
        action: "get_practice_help_status",
        dataObj: {
          num:11
        }
      }
    }).then(res => {
      for(let index in res.result){
        if(res.result[index]._id==1){
          that.setData({
            PracticeStatus:res.result[index].status
          })
        }
      }
    })
  },
  xGetHelp:function(){
    let that =this;
    let _dataObj = {
      num:11,//练习任务类型数值
      type:1,//1:练习说明
    }
    wx.cloud.callFunction({
      name: 'practice_help',
      data: {
        action: "get_practice_help",
        dataObj: _dataObj
      }
    }).then(res => {
      
      res.result[0].content=text.text(res.result[0].content)
      that.setData({
        practice_help:res.result[0]
      }) 
    })
  },
  xTosetM:function(){
    
    this.setData({
      showtips:!this.data.showtips
    }) 
    wx.setStorageSync(this.data.treatment_item_id, this.data.treatment_item_id)
  },
  xSetM:function(){
    this.setData({
      is_write:!this.data.is_write
    })
    wx.pageScrollTo({
      duration: 0,
      scrollTop:0
    })
  },
  xTonext:function(){
    let that =this;
    let key =0;
    if(that.data.step==0){
      if(that.data.full_name=="") { key=key+1 }
      if(that.data.communication_object==""){  key=key+1 }
      if(that.data.communicate_problems==""){  key=key+1 }
      if(that.data.full_name!==''&& that.data.communication_object!==''&& that.data.communicate_problems!=='' ){
        
      }else{
        wx.showModal({
          title: "提示",
          content: "请确保所有内容填写正确，当前有"+ key +"项内容未填写，请检查！",
          showCancel: false,
          confirmText:'好的'
        })
        return
      }
    }else if(that.data.step==1){
      
    }
    that.setData({
      step:that.data.step+1
    })
  },
  xSet_page:function(e){
    this.setData({
      show:true,
      status:e.currentTarget.dataset.status
    })
  },
  //设定关系
  xPicker: function (e) {
    let that = this
    let sel = e.detail.value
    let range = that.data.range
    that.setData({
      communication_object: range[sel]
    })
    wx.setStorageSync('communication_object', range[sel])
  },
  // 称呼
  bindInput:function(e){
    let that =this;
    that.setData({
      full_name:e.detail.value,
    })
    wx.setStorageSync('full_name', e.detail.value)
  },
  // 沟通问题
  xSetInputcommunicate_problems:function(e){
    this.setData({
      communicate_problems:e.detail.value
    })
    wx.setStorageSync('communicate_problems', e.detail.value)
  },
  // 沟通预期
  xSetInputcommunicate_expectations:function(e){
    this.setData({
      communicate_expectations:e.detail.value
    })
    wx.setStorageSync('communicate_expectations', e.detail.value)
  },
  //范围
  xsetcontrollable:function(e){
    this.setData({
      is_controllable:e.currentTarget.dataset.is_controllable
    })
    wx.setStorageSync('is_controllable', e.currentTarget.dataset.is_controllable)
  },
  // 不可控分析
  xSetInputexpectation_analysis:function(e){
    this.setData({
      expectation_analysis:e.detail.value
    })
    wx.setStorageSync('expectation_analysis', e.detail.value)
  },
  // 表达
  xSetInputsay:function(e){
    this.setData({
      say:e.detail.value
    })
    wx.setStorageSync('say', e.detail.value)
  },
  // 概率
  sliderchange:function(e){
    this.setData({
      success_probability:e.detail.value
    })
    wx.setStorageSync('success_probability', e.detail.value)
  },
  xSave:function(){
    let that =this;
    let key=0;
    if(getApp().globalData.lock==true){
      return
    }
    if(that.data.communicate_expectations==""){
      key=key+1
    }
    if(that.data.is_controllable==""){
      key=key+1
    }
    if(that.data.say==""){
      key=key+1
    }
    if(that.data.success_probability==""){
      key=key+1
    }
    if(that.data.success_probability!==''&& that.data.say!==''&& that.data.is_controllable!==''&& that.data.communicate_expectations!=='' ){
      
    }else{
      wx.showModal({
        title: "提示",
        content: "请确保所有内容填写正确，当前有"+ key +"项内容未填写，请检查！",
        showCancel: false,
        confirmText:'好的'
      })
      return
    }
    let _dataObj = {
      treatment_item_id:that.data.treatment_item_id,//任务id
      week:that.data.week,//第几个任务
      communication_object:that.data.communication_object,
      full_name:that.data.full_name,
      communicate_expectations:that.data.communicate_expectations,
      is_controllable:Number(that.data.is_controllable),
      expectation_analysis:that.data.expectation_analysis,
      say:that.data.say,
      success_probability:Number(that.data.success_probability),
      communicate_problems:that.data.communicate_problems,
    }
    if(JSON.parse(that.data.today_communication_skills_status)==true){
      _dataObj._id = that.data._id
      getApp().globalData.lock=true
      wx.showLoading({
        title: '保存中',
      })
      that.xToUpdate(_dataObj)
    }else{
      getApp().globalData.lock=true
      wx.showLoading({
        title: '保存中',
      })
      that.xToAdd(_dataObj)
    }
  },
  xChildReload: function () {
    var pages = getCurrentPages();//当前页面栈
    if (pages.length > 1) {
      var beforePage = pages[pages.length - 2];//获取上一个页面实例对象
      beforePage.xInfo();//触发父页面中的方法
    }
  },
  xToUpdate:function(_dataObj){
    let that =this;
    wx.cloud.callFunction({
      name: 'communication_skills',
      data: {
        action: "update_communication_skills",
        dataObj: _dataObj
      }
    }).then(res => {
          wx.hideLoading()
          wx.showToast({
            title: '成功',
            icon:'none',
          })
          
          var timer = setTimeout(() => {
              getApp().globalData.lock=false
              wx.navigateBack({
                delta:1
              })
              that.xChildReload()
          }, 3000);
        
    }).catch(err=>{
      wx.hideLoading()
      getApp().globalData.lock=false
    })
  },
  xToAdd:function(_dataObj){
    let that =this;
    wx.cloud.callFunction({
      name: 'communication_skills',
      data: {
        action: "add_communication_skills",
        dataObj: _dataObj
        
      }
    }).then(res => {
          wx.hideLoading()
          wx.showToast({
            title: '成功',
            icon:'none',
          })
          var timer = setTimeout(() => {
            getApp().globalData.lock=false
              wx.navigateBack({
                delta:1
              })
              that.xChildReload()
          }, 3000);
        
    }).catch(err=>{
      wx.hideLoading()
      getApp().globalData.lock=false
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})